from EXFAT.Constant import *


class File:
    def __init__(self, name: str, size: int, local: str, file_type: str, parent: str = None):
        self.name = name
        self.size = size
        self.local = local
        self.file_type = self.__determine_type(file_type)
        self.children = {'folders': [], 'files': []}
        self.parent = parent
        self.data = ""
        self.checked = False

    def __determine_type(self, file_type: str):
        if file_type == 16:
            return IS_FOLDER
        split = self.name.split(".")
        name_suffix = split[-1].lower()
        if name_suffix in SUFFIX_LIST and len(split) > 1:
            return SUFFIX_LIST.index(name_suffix)
        return UNKNOWN_TYPE

    def hasChildren(self):
        return len(self.children['folders'])>0 or len(self.children['files'])>0

    def getFiles(self):
        return self.children['files']

    def getFolders(self):
        return self.children['folders']

    def __str__(self):
        return f"<文件名：{self.name} ,文件类型：{self.file_type},子文件：{self.children},父文件：{self.parent},所在簇区：{self.local}>"

    def __repr__(self):
        return f"<文件名：{self.name}"
